package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import zhihuilan.entity.MyCart;
import zhihuilan.entity.MyCart;
import zhihuilan.mapper.MyCartMapper;
import zhihuilan.params.MyCartDeleteParam;
import zhihuilan.params.query.ShopCartQueryParam;
import zhihuilan.service.MyCartService;

import java.util.List;

@Service
public class MyCartServiceImpl extends ServiceImpl<MyCartMapper, MyCart> implements MyCartService {


    @Override
    public List<MyCart> selectList(ShopCartQueryParam queryParam) {
        if (ObjectUtils.isEmpty(queryParam)) {
            return null;
        }
        QueryWrapper<MyCart> wrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam.getShopid())) {
            wrapper.eq("shopid", queryParam.getShopid());
        }
        if (!ObjectUtils.isEmpty(queryParam.getStaffId())) {
            wrapper.eq("staffid", queryParam.getStaffId());
        }
        if (!ObjectUtils.isEmpty(queryParam.getShopmemberid())) {
            wrapper.eq("shopmemberid", queryParam.getShopmemberid());
        }
        if (!ObjectUtils.isEmpty(queryParam.getProductid())) {
            wrapper.eq("productid", queryParam.getProductid());
        }
        if (!ObjectUtils.isEmpty(queryParam.getPricetype())) {
            wrapper.eq("pricetype", queryParam.getPricetype());
        }
        if (!ObjectUtils.isEmpty(queryParam.getSource())) {
            wrapper.eq("source", queryParam.getSource());
        }
        if (!ObjectUtils.isEmpty(queryParam.getSourceType())) {
            wrapper.eq("source_type", queryParam.getSourceType());
        }
        return this.baseMapper.selectList(wrapper);
    }

    /**
     * 查詢购物车列表
     */
    @Override
    public List<MyCart> selectList(Integer staffId, Integer sourceType, Integer shopid) {
        QueryWrapper<MyCart> wrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(staffId)) {
            wrapper.eq("staffid", staffId);
        }
        if (!ObjectUtils.isEmpty(sourceType)) {
            wrapper.eq("source_type", sourceType);
        }
        if (!ObjectUtils.isEmpty(shopid)) {
            wrapper.eq("shopid", shopid);
        }
        return this.baseMapper.selectList(wrapper);
    }

    /**
     * 按照ID查询对应的购物车物品
     */
    @Override
    public MyCart selectById(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return null;
        }
        return this.baseMapper.selectById(id);
    }

    /**
     * 添加购物车物品
     */
    @Override
    public MyCart addEntity(MyCart entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        this.baseMapper.insert(entity);
        return entity;
    }

    /**
     * 清空购物车商品，兵添加新的列表；
     * 这里使用事务的方式新增，先删除购物车数据，再将需要添加的商品列表添加到数据库，然后执行提交；若其中有任何一个场景失败，则整个事务回滚；
     */
    @Transactional
    @Override
    public Boolean addEntitys(List<MyCart> entitys) throws RuntimeException {
        try {
            if (ObjectUtils.isEmpty(entitys)) {
                return false;
            }
            if (entitys.size() < 1) {
                return false;
            }
            Integer staffId = entitys.get(0).getStaffid();
            Integer sourceType = entitys.get(0).getSourceType();
            this.deletetByParam(staffId, sourceType);
            return this.saveBatch(entitys);
        } catch (RuntimeException e) {
            throw new RuntimeException("添加购物车数据执行异常，已回滚数据,异常原因：" + e.getMessage());
        }
    }

    /**
     * 更新购物车物品
     */
    @Override
    public MyCart updateEntity(MyCart entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        this.baseMapper.updateById(entity);
        return entity;
    }

    /**
     * 更新购物车物品
     */
    @Override
    public Boolean updateEntitys(List<MyCart> entitys) {
        if (ObjectUtils.isEmpty(entitys)) {
            return false;
        }
        if (entitys.size()==0) {
            return false;
        }
        return this.saveOrUpdateBatch(entitys);
    }

    @Override
    public Boolean updateQueryParam(MyCart entity,ShopCartQueryParam queryParam){
        QueryWrapper<MyCart> wrapper = new QueryWrapper<>();
        if(ObjectUtils.isEmpty(entity)||ObjectUtils.isEmpty(queryParam)){
            return null;
        }
        if (!ObjectUtils.isEmpty(queryParam.getStaffId())) {
            wrapper.eq("staffid", queryParam.getStaffId());
        }
        if (!ObjectUtils.isEmpty(queryParam.getShopid())) {
            wrapper.eq("shopid", queryParam.getShopid());
        }
        if (!ObjectUtils.isEmpty(queryParam.getShopmemberid())) {
            wrapper.eq("shopmemberid", queryParam.getShopmemberid());
        }
        if (!ObjectUtils.isEmpty(queryParam.getSource())) {
            wrapper.eq("source", queryParam.getSource());
        }
        return this.baseMapper.update(entity,wrapper)>0;
    }

    /**
     * 根据主键删除购物车物品
     */
    @Override
    public Boolean deletetById(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return false;
        }
        int result = this.baseMapper.deleteById(id);
        return result > 0;
    }

    /**
     * 根据条件删除购物车物品列表
     *
     * @param staffId    用户ID
     * @param sourceType 商品类型；1、厂家，2、市场
     */
    @Override
    public Boolean deletetByParam(Integer staffId, Integer sourceType) {
        QueryWrapper<MyCart> wrapper = new QueryWrapper<>();
        if (ObjectUtils.isEmpty(staffId)) {
            return false;
        }
        if (ObjectUtils.isEmpty(sourceType)) {
            return false;
        }
        wrapper.eq("staffid", staffId);
        wrapper.eq("source_type", sourceType);
        int result = this.baseMapper.delete(wrapper);
        return result > 0;
    }

    @Override
    public Boolean deleteParam(MyCartDeleteParam deleteParam){
        QueryWrapper<MyCart> wrapper = new QueryWrapper<>();
        if (ObjectUtils.isEmpty(deleteParam)) {
            return false;
        }
        if (!ObjectUtils.isEmpty(deleteParam.getStaffid())) {
            wrapper.eq("staffid", deleteParam.getStaffid());
        }
        if(!ObjectUtils.isEmpty(deleteParam.getSource())){
            wrapper.eq("source", deleteParam.getSource());
        }
        if(!ObjectUtils.isEmpty(deleteParam.getShopid())){
            wrapper.eq("shopid", deleteParam.getShopid());
        }
        if(!ObjectUtils.isEmpty(deleteParam.getShopmemberid())){
            wrapper.eq("shopmemberid", deleteParam.getShopmemberid());
        }
        int result = this.baseMapper.delete(wrapper);
        return result > 0;
    }

}
